//
//  ZJRoomCell.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/12.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJRoomCell.h"
static NSMutableArray * modelArr;
@implementation ZJRoomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    modelArr = [NSMutableArray new];
}

-(void)setModel:(ZJCeillingsModel *)model{
    _model = model;

    self.floorTitle.text = _model.ceiling_name;
 //   if (self.scrollview.subviews.count <= 0 || !self.scrollview.subviews || self.scrollview.subviews.count != _model.houseNum.count) {
        
        NSArray *subViews = [self.scrollview subviews];
        
        if([subViews count] != 0) {
            
            [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
        }
        self.scrollview.bounces = NO;
        self.scrollview.pagingEnabled = YES;
        float btnWidth = (SCREEN_WIDTH - 75) /5;
        float btnHeight = self.scrollview.frame.size.height ;
        NSInteger space =((SCREEN_WIDTH - 75) - (SCREEN_WIDTH - 75 - 20))/5;
        
        float width;
        
        if (_model.houses.count % 5 != 0) {
            width = SCREEN_WIDTH - 75 ;
        }else{
            width = 0;
        }
        
        self.scrollview.contentSize = CGSizeMake((_model.houses.count/5) * self.scrollview.frame.size.width + width + 50, 0);
        
        modelArr = [ZJRoomModel mj_objectArrayWithKeyValuesArray:_model.houses];
    
//    if (subViews.count != modelArr.count) {
      
            for (int i = 0; i < _model.houses.count; i++) {
                UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i * (btnWidth + space), 0, btnWidth, btnHeight)];
                
                _roomModel = modelArr[i];
                [btn setTitle:_roomModel.house_name forState:0];
                [btn setBackgroundColor:[UIColor whiteColor]];
                btn.titleLabel.textAlignment = NSTextAlignmentCenter;
                [btn setTitleColor:[UIColor grayColor] forState:0];
                btn.titleLabel.numberOfLines = 0;
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
                btn.tag = i;
                [btn addTarget:self action:@selector(clickBtn:) forControlEvents:(UIControlEventTouchUpInside)];
                
                btn.layer.masksToBounds = YES;
                btn.layer.cornerRadius = 4.0f;
                btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
                btn.layer.borderWidth = 1.0f;
                
                if ([_roomModel.status integerValue] > 1) {
                    btn.layer.borderColor = [UIColor redColor].CGColor;
                    [btn setTitleColor:[UIColor redColor] forState:0];
                    [btn setTitle:[NSString stringWithFormat:@"%@\n已被抢",_roomModel.house_name] forState:0];
                    btn.titleLabel.font = [UIFont systemFontOfSize:12];
                    }
  
                if ([_roomModel.status integerValue] == 0) {
                    [btn setTitle:[NSString stringWithFormat:@"%@\n未开售",_roomModel.house_name] forState:0];
                    btn.layer.borderColor = RGB(215, 215, 215).CGColor;
                    [btn setTitleColor:RGB(215, 215, 215) forState:0];
                    btn.titleLabel.font = [UIFont systemFontOfSize:12];
                }
                    [self.scrollview addSubview:btn];
                }

//        }
    
}

-(void)clickBtn:(UIButton *)sender{

    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClickRoom:)]) {
        NSArray * arr = [ZJRoomModel mj_objectArrayWithKeyValuesArray:_model.houses];
        _roomModel = arr[sender.tag];
        [_delegate onClickRoom:_roomModel];
    }
}

-(void) onClickRoom:(ZJRoomModel *) roomDict{

    if (_delegate != nil && [_delegate respondsToSelector:@selector(onClickRoom:)]) {
        [_delegate onClickRoom:roomDict];
    }
}
@end
